package com.mi.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.mi.bean.ShoppingcartBean;
import com.mi.service.impl.AddGoodsService;
import com.mi.utils.MyUtils;

/**
 * 查询购物车，显示信息
 */
@WebServlet("/QueryShopcartAction")
public class QueryShopcartAction extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * 查询用户的购物车信息，并范湖数据
	 */
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		// 获取用户id
		String customer_id = (String)request.getSession().getAttribute("sid");
		// 查询该用户的购物车信息
		List<ShoppingcartBean> lstShopcartInfo = MyUtils.getInstance(AddGoodsService.class).queryShopInfo(customer_id);
		
		String strJson = JSON.toJSONStringWithDateFormat(lstShopcartInfo, "yyyy-MM-dd hh:mm:ss");
		// ajax响应
		// 响应客户单的请求
		PrintWriter pw = response.getWriter();
		// 将json字符串发回客户端浏览器
		pw.write(strJson);
		pw.flush();
		
		System.out.println(lstShopcartInfo.size());
		// 获取订单总额
		int count = 0;
		for(int i = 0;i < lstShopcartInfo.size();i++) {
			count += lstShopcartInfo.get(i).getGoods_price();
		}
		System.out.println("总计："+count);
		request.getSession().setAttribute("total", count);
	}

}
